//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.Provenance.Controls.View
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using Microsoft.Research.Provenance.Controls.Presenter;
    using Microsoft.Research.ProvenanceCommon.Model;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Monitor = Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor;

    /// <summary>
    /// Interaction logic for WorkFlowInputView.xaml
    /// </summary>
    public partial class WorkflowOutputView : UserControl
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public WorkflowOutputView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the WorkflowOutputPresenter Model to the view's data context.
        /// </summary>
        public WorkflowOutputPresenter Presenter
        {
            get { return this.DataContext as WorkflowOutputPresenter; }
            set { this.DataContext = value; }
        }

        /// <summary>
        /// The event handler for expanded control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">RoutedEventArgs.</param>
        private void OnExpanded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!this.Presenter.IsUpdated)
            {
                this.Presenter.GetWorkflowOutputs.Execute(null);
            }
        }
        
        /// <summary>
        /// Fired on link click.
        /// </summary>
        /// <param name="sender">
        /// Link.
        /// </param>
        /// <param name="e">
        /// MouseButtonEventArgs.
        /// </param>
        private void OnLinkClick(object sender, MouseButtonEventArgs e)
        {
            TextBlock link = sender as TextBlock;
            if (link != null)
            {
                ActivityProperty outputProperty = ((ContentPresenter)(link.TemplatedParent)).DataContext as ActivityProperty;
                if (outputProperty != null)
                {
                    Monitor.DataProductItem dataproductView = outputProperty.Data;
                    if (dataproductView != null)
                    {
                        link.Foreground = Brushes.BlueViolet;
                        try
                        {
                            Process.Start(dataproductView.HtmlPath);
                        }
                        catch (IOException ex)
                        {
                            TridentErrorHandler.HandleUIException(ex);
                        }
                        catch (System.ComponentModel.Win32Exception ex)
                        {
                            TridentErrorHandler.HandleUIException(ex);
                        }
                        catch (ObjectDisposedException ex)
                        {
                            TridentErrorHandler.HandleUIException(ex);
                        }
                        catch (Exception ex)
                        {
                            TridentErrorHandler.HandleUIException(ex);
                        }
                    }
                }
            }
        }
    }
}
